
export const baseURL = "http://38p869s402.zicp.vip"
export const request = (params) => {
  let method = "get";
  let header = {};
  if (params.method) method = params.method;
  if (params.header) header = params.header;
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + params.url,
      data: params.data,
      method: method,
      header: {
        ...header,
      },
      success(res) {
        if (res.statusCode === 200) {
          if(res.data.code === 401) {
            reject(res.data)
          }else if (res.data.code !== 200) {
            wx.showToast({
              title: res.data.msg || '服务异常',
              icon: 'error'
            })
            reject(res.data)
            return
          }
          resolve(res.data)
        } else {

          wx.showToast({
            title: res.errMsg || '服务异常',
            icon: 'error'
          })
          reject(res)
        }
      },
      fail(err) {
        wx.showToast({
          title: err.errMsg || '服务异常',
          icon:'error'
        })
        reject(err);
      }
    })
  })
}

export const uploadFile = (params) => {
  return new Promise((reslove, reject) => {
    wx.uploadFile({
      url: `${baseURL}/sb/common/upload`,
      filePath: params.filePath,
      formData: params.data || {},
      name: params.name || 'file',
      header: {
        "AU-Authorization": wx.getStorageSync('token') || '',
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const data = JSON.parse(res.data)
          if (data.code !== 200) {
            wx.showToast({
              title:data.msg || '服务异常',
              icon: 'error'
            })
            reject(data)
            return
          }
          reslove(data)
        } else {
          wx.showToast({
            title:res.errMsg || '服务异常',
            icon: 'error'
          })
          reject(res)
        }
      },
      fail: err => {
        wx.showToast({
          title: err.errMsg || '服务异常',
          icon: 'error'
        })
        reject(err);
      }
    })
  })
}

export function getImageURL(path) {
  return baseURL+'/sb/profile/' + path
}

export function getFileName(path){
  return path.split('/').pop()
}